//
//  NoticeListViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/9.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "NoticeListViewController.h"
#import "NoticeListModel.h"
#import "NoticeListTableViewCell.h"
#import "NoticeDetailViewController.h"


@interface NoticeListViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, strong)NSMutableArray * dataSource;
@property(nonatomic, strong)ExceptionView * exceptionView;


@end

@implementation NoticeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setProperty];
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    [self Http_notice_list];
    
}

-(void)setProperty{
    
    _dataSource = [[NSMutableArray alloc]init];
    _page=1;

    self.view.backgroundColor=RGB(255, 255, 255);
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    self.baseVcTitle = @"公告列表";
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.bgView addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(0);
        make.left.equalTo(self.bgView).offset(0);
        make.right.equalTo(self.bgView).offset(0);
        make.bottom.equalTo(self.bgView).offset(0);
    }];
    
}


#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataSource.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [NoticeListTableViewCell computeHeight:@""];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *reuseIdentifier =[NSString stringWithFormat:@"NoticeListTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    
    NoticeListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[NoticeListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (_dataSource.count>0) {
        cell.dataSource=_dataSource[indexPath.row];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_dataSource.count>0) {
        NoticeDetailViewController * VC = [[NoticeDetailViewController alloc]init];
        VC.NLModel = _dataSource[indexPath.row];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
}

/*
 *
 
 获取指定分类下的公告
 
 1、接口地址
 
 请求方式：post
 请求接口：notice/list
 需要登录：否
 2、接口请求参数
 
 参数名    参数类型    是否必须    含义    备注
 category_id    整型    是    分类id
 page    整型    否    分页页码
 limit    整型    否    每页显示数量
 */
#pragma mark-公告分类
-(void)Http_notice_list{
    
    
    NSString * URL = [NSString stringWithFormat:@"%@notice/list",DEFAULTSEVERBASEURL];
    NSDictionary * Dic = @{
                           @"page":[NSNumber numberWithInteger:_page],
                           @"limit":@"20",
                           @"category_id":_NCModel.ID
                           };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self.bgView];
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf.bgView];
        
        
        NSDictionary * dic = responseObject;
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            NSDictionary * dicc = dic[@"data"];
            NSArray * arr = dicc[@"list"];
            
            [weakSelf.dataSource removeAllObjects];
            for (NSDictionary * temp in arr) {
                
                NoticeListModel * model = [[NoticeListModel alloc]init];
                model.create_time = [NSString stringWithFormat:@"%@",temp[@"create_time"]];
                model.ID = [NSString stringWithFormat:@"%@",temp[@"id"]];
                model.category_id = [NSString stringWithFormat:@"%@",temp[@"category_id"]];
                model.title = [NSString stringWithFormat:@"%@",temp[@"title"]];
                model.is_top = [NSString stringWithFormat:@"%@",temp[@"is_top"]];
                model.category = temp[@"category"];

                [weakSelf.dataSource addObject:model];
            }
            [weakSelf.tableView reloadData];
            if (weakSelf.dataSource.count<1) {
                [weakSelf.exceptionView dismiss];
                weakSelf.exceptionView = nil;
                weakSelf.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
                weakSelf.exceptionView.heightH = G_SCREENHEIGHT-kStatusBarAndNavigationBarHeight;
                [weakSelf.exceptionView showInView:weakSelf.bgView];
            }
            
            
        }else{
            [HUD showErrorMessage:msg];
        }
    } FailureBlock:^(id  _Nonnull error) {
        [HUD dismissInView:weakSelf.bgView];
    }];
    
   
    
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
